package com.example.education.controller;

import com.example.education.exception.ParamException;
import com.example.education.model.Question;
import com.example.education.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/interface/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @RequestMapping("/insert")
    public boolean insertQuestion(Question question){
        boolean result = false;
        try {
            questionService.insertQuestion(question);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/delete")
    public boolean deleteQuestion(Integer questionId){
        boolean result = false;
        try {
            questionService.deleteQuestion(questionId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/top")
    public boolean topQuestion(Integer questionId){
        boolean result = false;
        try {
            questionService.topQuestion(questionId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/unTop")
    public boolean unTopQuestion(Integer questionId){
        boolean result = false;
        try {
            questionService.unTopQuestion(questionId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/getById")
    public Question getQuestionById(Integer questionId){
        Question question = new Question();
        try {
            question = questionService.getQuestionById(questionId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return question;
    }

    @RequestMapping("/getByCourseId")
    public List<Question> getQuestionByCourseId(Integer courseId){
        List<Question> questions = new ArrayList<>();
        try {
            questions = questionService.getQuestionByCourseId(courseId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return questions;
    }
}
